﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class SiteWorx_WebParts_Advanced_RssFeedLink_RssFeedLinkEditor : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
            LoadEditor();
    }

    private void LoadEditor()
    {
        string rssFeedLinkWPDataId = Request.QueryString["rssFeedLinkWPDataId"].ToString();

        if (Session[rssFeedLinkWPDataId] != null)
        {
            RssFeedLinkWebPartData data = (RssFeedLinkWebPartData)Session[rssFeedLinkWPDataId];

            txtChannelName.Text = data.ChannelName;
            txtNavigateUrl.Text = data.NavigateUrl;
            lstTarget.Items.FindByValue(data.Target).Selected = true;
            lstHorizonalAlign.Items.FindByValue(data.RssLinkHorizontalAlign.ToString());
            txtLinkText.Text = data.Text;
            txtHoverText.Text = data.HoverText;
            chkDisplayImage.Checked = data.DisplayImage;
            chkVisible.Checked = data.RssVisible;
        }
    }

    protected void btnFinish_Click(object sender, EventArgs e)
    {
        string rssFeedLinkWPDataId = Request.QueryString["rssFeedLinkWPDataId"].ToString();

        ((RssFeedLinkWebPartData)Session[rssFeedLinkWPDataId]).ChannelName = txtChannelName.Text;
        ((RssFeedLinkWebPartData)Session[rssFeedLinkWPDataId]).NavigateUrl = txtNavigateUrl.Text;
        ((RssFeedLinkWebPartData)Session[rssFeedLinkWPDataId]).Target = lstTarget.SelectedValue;
        ((RssFeedLinkWebPartData)Session[rssFeedLinkWPDataId]).RssLinkHorizontalAlign = (HorizontalAlign)(int.Parse(lstHorizonalAlign.SelectedValue.ToString()));
        ((RssFeedLinkWebPartData)Session[rssFeedLinkWPDataId]).Text = txtLinkText.Text;
        ((RssFeedLinkWebPartData)Session[rssFeedLinkWPDataId]).HoverText = txtHoverText.Text;
        ((RssFeedLinkWebPartData)Session[rssFeedLinkWPDataId]).DisplayImage = chkDisplayImage.Checked;
        ((RssFeedLinkWebPartData)Session[rssFeedLinkWPDataId]).RssVisible = chkVisible.Checked;
       
        BuildUpdateScript(rssFeedLinkWPDataId);
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        string rssFeedLinkWPDataId = Request.QueryString["rssFeedLinkWPDataId"].ToString();
        BuildUpdateScript(rssFeedLinkWPDataId);
    }

    private void BuildUpdateScript(string rssFeedLinkWPDataId)
    {
        string functionName = "Update_" + rssFeedLinkWPDataId.Replace("-", "_");
        string script = "var wnd = getRadWindow(); wnd.BrowserWindow." + functionName + "(wnd); wnd.close();";

        ScriptManager.RegisterStartupScript(this, this.GetType(), "UpdateParentWebPart", script, true);
    }
}
